//
//  LanguageController.m
//  TabNav
//
//  Created by Kalyan Mudumbai on 8/28/10.
//  Copyright 2010 GOLDMAN SACHS. All rights reserved.
//

#import "LanguageController.h"
#import "StotramController.h"
#import "PDFViewController.h"


@implementation LanguageController
@synthesize languageArray;

-(void) viewDidLoad {
	self.languageArray = [[NSArray alloc] initWithObjects:@"Telugu",@"English",@"Telugu PDF",@"Tamil PDF", @"Kannada PDF", @"Malayalam PDF", 
						  @"Devanagari PDF", @"Bengali PDF", @"Roman PDF",nil];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [languageArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = [languageArray objectAtIndex:[indexPath row]];
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	NSUInteger selectedRow = [indexPath row];
	NSLog(@"Selected language is: %@",[languageArray objectAtIndex:selectedRow]);
	
	NSString * selectedLang = [languageArray objectAtIndex:selectedRow];
	
	if([selectedLang isEqual:@"English"]) {
		NSLog(@"English is selected");
	}
	
	if(selectedRow > 1) {
		NSString * pdfFileName = @"vsn-roman";
		
		if([selectedLang isEqual:@"Telugu PDF"]) {
			pdfFileName = @"vsn-telugu";
		} else if ([selectedLang isEqual:@"English PDF"]) {
			pdfFileName = @"vsn-roman";
		} else if ([selectedLang isEqual:@"Tamil PDF"]) {
			pdfFileName = @"vsn-tamil";
		} else if ([selectedLang isEqual:@"Bengali PDF"]) {
			pdfFileName = @"vsn-bengali";
		} else if ([selectedLang isEqual:@"Devanagari PDF"]) {
			pdfFileName = @"vsn-devanagari";
		} else if ([selectedLang isEqual:@"Kannada PDF"]) {
			pdfFileName = @"vsn-kannada";
		} else if ([selectedLang isEqual:@"Malayalam PDF"]) {
			pdfFileName = @"vsn-malayalam";
		}
		
		// Create an instance of PDFViewController
		PDFViewController *controller = [[PDFViewController alloc] initWithNibName:@"PDFView" bundle:nil];
		NSString *pdfPath = [[NSBundle mainBundle] pathForResource:pdfFileName ofType:@"pdf"];
		NSLog(@"PDF Path is %@",pdfPath);
		controller.pdfUrl = [NSURL fileURLWithPath:pdfPath];
		// Push 'controller'
		[self.navigationController pushViewController:controller animated:YES];
		// Release 'controller'
		[controller release];		
	} else {
		StotramController * sc = [[StotramController alloc] init];
		sc.selectedLanguage = selectedLang;
		//sc.language = [imageArray objectAtIndex:[indexPath row]];
		[self.navigationController pushViewController:sc animated:YES];
		[sc release];
	}
}

@end
